package com.sskj.mine.asset;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.asset.data.AssetBean;
import com.sskj.mine.asset.data.AssetMainBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.invite.data.CommonBean;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Hey
 * Create at  2019/05/03
 */
@Route(path = RConfig.MINE_ASSETS)
public class AssetHomeFragment extends BaseFragment<AssetHomePresenter> {
    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.tvTitle)
    TextView titleTv;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.asset_tv)
    TextView assetTv;
    @BindView(R2.id.asset_unit)
    TextView assetUnit;
    @BindView(R2.id.asset_cny_tv)
    TextView assetCnyTv;
    @BindView(R2.id.exchange_tv)
    TextView exchangeTv;
    @BindView(R2.id.withdraw_tv)
    TextView withdrawTv;
    @BindView(R2.id.recharge_tv)
    TextView rechargeTv;
    @BindView(R2.id.tvTransfer)
    TextView tvTransfer;
    @BindView(R2.id.asset_list)
    RecyclerView assetList;
    @Inject
    UserViewModel userViewModel;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    private SlimAdapter adapter;
    List<AssetBean> data;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_fragment_asset_home;
    }

    @Override
    public AssetHomePresenter getPresenter() {
        return new AssetHomePresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);
        titleTv.setText(getString(R.string.mine_assetHomeFragment1));
        rightTv.setText(getString(R.string.mine_assetHomeFragment2));
        ivBack.setVisibility(View.INVISIBLE);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });

        rightTv.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.mine_icon_records, 0, 0, 0);
        assetList.setLayoutManager(new LinearLayoutManager(getContext()));
        assetList.addItemDecoration(new DividerLineItemDecoration(getContext())
                .setFirstDraw(false)
                .setDividerColor(color(R.color.mine_divider)));
        adapter = SlimAdapter.create().register(R.layout.mine_item_asset, new SlimInjector<AssetBean>() {
            @Override
            public void onInject(AssetBean data, IViewInjector injector, List<Object> list) {
                if (injector.pos() == 0) {
                    injector.findViewById(R.id.item_view).setBackgroundResource(R.mipmap.mine_usdt_bg);
                } else {
                    injector.findViewById(R.id.item_view).setBackgroundResource(R.mipmap.mine_coin_bg);
                }
                injector
                        .text(R.id.name_tv, String.format("%s ≈%sCNY", data.getPname(), CoinUtil.keepRMB(data.getRmb())))
                        .text(R.id.available_tv, CoinUtil.keepUSDT(data.getUsable()))
                        .text(R.id.freeze_tv, data.getLock_num())
                        .imageCircle(R.id.ivCoin, getResources().getDrawable(injector.pos() == 0 ? R.mipmap.mine_usdt : R.mipmap.mine_db));
            }

        }).attachTo(assetList);
    }


    @Override
    public void initData() {
        mPresenter.getAssetMain();
        mPresenter.getAssetMainList();
    }

    @Override
    protected void initEvent() {
        // 下拉刷新
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getAssetMain();
                mPresenter.getAssetMainList();
            }
        });
        //账单
        ClickUtil.click(rightTv, () -> BillActivity.start(getContext()));
        //提币
        ClickUtil.click(withdrawTv, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            WithdrawActivity.start(getContext());
        });
        //充币
        ClickUtil.click(rechargeTv, () -> RechargeActivity.start(getContext()));
        //兑换
        ClickUtil.click(exchangeTv, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            ExchangeActivity.start(getContext());
        });
        //转账
        ClickUtil.click(tvTransfer, () -> {
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            TransferActivity.start(getContext());
        });
    }

    public static AssetHomeFragment newInstance() {
        AssetHomeFragment fragment = new AssetHomeFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden && userData != null && mPresenter != null) {
            mPresenter.getAssetMain();
            mPresenter.getAssetMainList();
        }
        super.onHiddenChanged(hidden);
    }

    public void updateUI(AssetMainBean data) {
        assetTv.setText(CoinUtil.keepUSDT(data.getTotal()));
        assetCnyTv.setText(String.format("≈%s CNY", CoinUtil.keepRMB(data.getTotal_cny())));
    }

    public void updateList(List<AssetBean> list) {
        adapter.updateData(list);
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
    }
}
